<!MILESTONE CALENDAR>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
	<html>
	<head>
        <link href="stylesheet.css" rel="stylesheet" type="text/css" />
	</head>
	<body>
	<?php
   
	function showCalendar($formYear, $formMonth, $formDay)
    {
	   // Get key day informations.
	   // We need the first and last day of the month and the actual day
       /*$todayFunction = $today;
         $firstDayFunction = $firstDay;
         $lastDayFunction = $lastDay;*/
    // parse string into current year, current month, current day
   
    $year = $formYear;
    $month = $formMonth;
    $day = $formDay;

        
    //Switch statement to match month name with current month number.
    $monthName = "";
    switch($month)
    {
        case 1: 
			$monthName = "January";
			break;
        case 2:
			$monthName = "Febuary";
			break;
        case 3:
			$monthName = "March";
			break;
        case 4:
			$monthName = "April";
			break;
        case 5:
			$monthName = "May";
			break;
        case 6:
			$monthName = "June";
			break;
        case 7:
			$monthName = "July";
			break;
        case 8:
			$monthName = "August";
			break;
        case 9:
			$monthName = "September";
			break;
        case 10:
			$monthName = "October";
			break;
        case 11:
			$monthName = "November";
			break;
        case 12: 
			$monthName = "December";
			break;
        default:			$monthName = "Error";
			break;

    }    
    
	   //gets first day and last day of month
	   $firstDay = getdate(mktime(0,0,0,$month,1,$year));
       if($firstDay['wday'] == 0)
           $firstDay['wday'] = 1;
	   $lastDay = getdate(mktime(0,0,0,$month+1,0,$year));
	
	   // Create a table with the necessary header informations
	  
	   echo ' <div align = \'center\'> <h3><b>'.$monthName."  ".$year."</b></h2></div>";
       echo '<table border = 3 width = 100% bgcolor = FFAD5C>';
	   echo '<tr class="days">'; 
	   echo ' <td width = 13%><b>Monday</b></td><td width = 13%><b>Tuesday</b></td><td width = 13%><b>Wednesday</b></td><td width =        13%><b>Thursday</b></td>';
	   echo ' <td width = 13%><b>Friday</b></td><td width = 13%><b>Saturday</b></td><td width = 13%><b>Sunday</b></td></tr>';
	
	   // Display the first calendar row with correct positioning
	   echo '<tr>'; //FORMAT FOR ALL CELLS NOT IN CURRENT MONTH AND FIRST WEEK
	   
	   
	   /****************************************/
	   /*************DATABASE STUFF**************/
       $username = 'root';//CHANGE TO USERNAME ON CURRENT DB MACHINE!
       $pw = '';//CHANGE TO PASSWORD ON CURRENT DB MACHINE!
       $con = mysqli_connect("localhost", $username, $pw, 'milestone');
		
       if (mysqli_connect_errno()) 
       {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
       }
		   
       //set time to the central time zone
       if ( !date_default_timezone_set ( 'America/Chicago' ) )
       {
           echo "could not set time zone!";
       }
			
       $date = new DateTime();
       $datetime_string = date_format($date, 'Y-m-d');

       /*****************************************/
			
		
		
	   for($i=1;$i<$firstDay['wday'];$i++)
       {
           //shade cells that are not apart of the current month.
	       echo '<td BGCOLOR = F5F5DC >&nbsp</td>'; 
	   }
	   
	   $actday = 0; //for denton cells
       $actday2 = 0; //for aubrey cells
	   
	   for($i=$firstDay['wday'];$i<=7;$i++) //loop through week
       {
	       $actday++; //increment day
		   
		  //create SQL formatted string for looking up date in database
		   if ($actday < 10)
		   {//if the day is a single digit we will need a '0' in front of the day
				$sqlDate = $year . "-" . $month . "-" . "0" . $actday;
			}
			else 
			{
				$sqlDate = $year . "-" . $month . "-" . $actday;
		    }
		   
		    $queryString = "SELECT * FROM events WHERE date = '" . $sqlDate . "'  AND location = 0";
		    $result = mysqli_query($con, $queryString);//returns FALSE if query fails
			
            $db_array = mysqli_fetch_array($result);
			
			//store the info from the SQL query in variables. Note that we don't need the info indexes 1 and 2 (date and location)
			$eventName = $db_array[0];
			$eventStaff = $db_array[3];
			$eventDescription = $db_array[4];
		
            $button = $actday . $monthName . $year . "denton"; //make unique button
		   
	        if ($actday == $day) 
            {
	           $class = ' class="actday"'; //for css purposes
	        } 
            else 
            {
               //blank for first few days that arnt apart of month
	           $class = '';
	        }
		   
             //DENTON FIRST WEEK OF MONTH
             //when add event button pushed send month, day, year, location to add_event page
	         echo "<td class = title $class BGCOLOR = FFE0C2><b>$actday</b>
             <i><b><div align = 'center'>Denton</div></b></i>";
             
			 //if there is a result from the DB for this day and location then print the info
			 if (isset($db_array))
			 {
                echo "<table border = '1' align = 'center' bgcolor = #FFD119>"; //if event exists then make a cell
                $eventArray = explode(",", $eventStaff);
               
                for($k=0; $k < sizeof($eventArray); $k++)
                {
                   if($eventArray[$k] == $_SESSION['username'])
                   {
                       
				       echo "<tr width = '100%'><td style=\"width: 10%\" bgcolor = #E6B800><b><u><div align =                                              'center'>$eventName</div></u></b></td></tr>";
                        echo "<tr width = '100%'><td style=\"width: 10%\" bgcolor = #FFD119><div align =                                                   'center'>$eventDescription</div></td></tr>";
                   }
                }
                
           
                echo "</table>";
             }
          
             if(isset($_POST[$button]))
             { 
                echo $button;
                $_SESSION['location'] = "DENTON"; //Pass event details to add_event.php
                $_SESSION['day'] = $actday;
                $_SESSION['month'] = $month;
                $_SESSION['year'] = $year;
                $_SESSION['test'] = "DENTON" . ": " . $month . "-" . $actday . "-" . $year;
                
                 ?>
                    <script>location.href='add_event.php'</script>;
                <?php
                exit;
             }
            
             echo "</td>";
	   }
	   
       echo "</tr>";
       echo "<tr>"; 
        //DISPLAY FOR FIRST WEEK SECOND ROW "AUBREY"
	   for($i=1;$i<$firstDay['wday'];$i++) //starting on first day of week
       {
           //shade cells that are not apart of the current month
	       echo '<td BGCOLOR = F5F5DC  >&nbsp</td>';
	   }
       
	   for($i=$firstDay['wday'];$i<=7;$i++)
       {
	   
           $sqlDate = $year . "-" . $month . "-" . $actday2;
			
	       $actday2++; //aubrey counter increment
		   
		   //create SQL formatted string for looking up date in database
		   if ($actday2 < 10)
		   {//if the day is a single digit we will need a '0' in front of the day
				$sqlDate = $year . "-" . $month . "-" . "0" . $actday2;
			}
			else 
			{
				$sqlDate = $year . "-" . $month . "-" . $actday2;
		   }
		   
		   $queryString = "SELECT * FROM events WHERE date = '" . $sqlDate . "'  AND location = 1";
		   $result = mysqli_query($con, $queryString);//returns FALSE if query fails
			
			$db_array = mysqli_fetch_array($result);
			
			//store the info from the SQL query in variables. Note that we don't need the info indexes 1 and 2 (date and location)
			$eventName = $db_array[0];
			$eventStaff = $db_array[3];
			$eventDescription = $db_array[4];
			
           $button = $actday2 . $monthName . $year . "aubrey"; //makes unique button names for each day of the month
	       if ($actday2 == $day) 
           {
	           $class = ' class="actday"';
	       } 
           else 
           {
               //blank for first few days that arnt apart of month
	           $class = '';
	       }
           //display events in cell for AUBREY
           //send month,day,year,locaiton to add_event page 
	         echo "<td class = title $class BGCOLOR =E0E0FF><b> $actday2</b>
             <i><b><div align = 'center'>Aubrey</div></b></i>";
			 
			//if there is a result from the DB for this day and location then print the info
			 if (isset($db_array))
			 {
                echo "<table border = '1' align = 'center' bgcolor = #ADD6FF>";
                $eventArray = explode(",", $eventStaff);
                 
                for($k=0; $k < sizeof($eventArray); $k++)
                {
                   if($eventArray[$k] == $_SESSION['username'])
                   {
                        echo "<tr width = '100%'><td style=\"width: 10%\" bgcolor = #5CADFF><b><u><div align =                                             'center'>$eventName</div></u></b></td></tr>";
				        echo "<tr width = '100%'><td style=\"width: 10%\" bgcolor = #ADD6FF><div align =                                             'center'>$eventDescription</div></td></tr>";
                   }
                }

                echo "</table>";
                
             }
             echo "</td>";
	   }
	   echo '</tr>';
        
        
        //-------------------------------------FOR REST OF MONETH--------------------------------------------------
        
	  
	   //Get how many complete weeks are in the actual month
	   $fullWeeks = floor(($lastDay['mday']-$actday)/7); 
	   for ($i=0;$i<$fullWeeks;$i++)
       {
           echo '<tr>'; //row for each new week
	        //FORMAT FOR ALL DENTON CELLS new row for every seven days
	       for ($j=0;$j<7;$j++)
           {
	           $actday++; //increment counter for DENTON
			   
			   //create SQL formatted string for looking up date in database
               if ($actday < 10)
			   {//if the day is a single digit we will need a '0' in front of the day
					$sqlDate = $year . "-" . $month . "-" . "0" . $actday;
               }
               else 
               {
					$sqlDate = $year . "-" . $month . "-" . $actday;
			   }
		   
               $queryString = "SELECT * FROM events WHERE date = '" . $sqlDate . "'  AND location = 0";
               $result = mysqli_query($con, $queryString);//returns FALSE if query fails 
               $db_array = mysqli_fetch_array($result);
			
               //store the info from the SQL query in variables. Note that we don't need the info indexes 1 and 2 (date and                        //location)
               $eventName = $db_array[0];
               $eventStaff = $db_array[3];
               $eventDescription = $db_array[4];
               $button = $actday . $monthName . $year . "denton";//makes unique button names for each day of the month
              
	           if ($actday == $day) 
               {
	               $class = ' class="actday"';
	           } 
               else 
               {
	               $class = '';
	           }
               //Display DENTON cell
	           echo "<td class = title $class BGCOLOR = FFE0C2><b>$actday</b>
               <i><b><div align = center>Denton</div></b></i>";
               $row = mysqli_fetch_array($result);
               
			   //if there is a result from the DB for this day and location then print the info
			   //if there is a result from the DB for this day and location then print the info
			   if (isset($db_array))
			   {
                    echo "<table border = '1' align = 'center' bgcolor = #FFD119>"; //if event exists then make a cell
                    $eventArray = explode(",", $eventStaff);
               
                    for($k=0; $k < sizeof($eventArray); $k++)
                    {
                        if($eventArray[$k] == $_SESSION['username'])
                        {
                       
				            echo "<tr width = '100%'><td style=\"width: 10%\" bgcolor = #E6B800><b><u><div align =                                             'center'>$eventName</div></u></b></td></tr>";
                            echo "<tr width = '100%'><td style=\"width: 10%\" bgcolor = #FFD119><div align =                                                   'center'>$eventDescription</div></td></tr>";
                        }
                    }
                    echo "</table>";
               }
               echo "</td>";    
			   if(isset($_POST[$button]))
               { 
                    $_SESSION['location'] = "DENTON"; //Pass event details to add_event.php
                    $_SESSION['day'] = $actday;
                    $_SESSION['month'] = $month;
                    $_SESSION['year'] = $year;
                    $_SESSION['test'] = "DENTON" . ": " . $month . "-" . $actday . "-" . $year;
                    ?>
                    <script>location.href='add_event.php'</script>;
                    <?php
                    exit;
               }
                
	       }
	       echo '</tr>';
           echo '<tr>'; //FORMAT FOR ALL AUBREY CELLS
	       for ($j=0;$j<7;$j++)
           {
		   
				$sqlDate = $year . "-" . $month . "-" . $actday2;
                $actday2++; //aubrey count increment 
				//create SQL formatted string for looking up date in database
				if ($actday2 < 10)
			    {//if the day is a single digit we will need a '0' in front of the day
					$sqlDate = $year . "-" . $month . "-" . "0" . $actday2;
				}
				else 
				{
					$sqlDate = $year . "-" . $month . "-" . $actday2;
			    }
				$queryString = "SELECT * FROM events WHERE date = '" . $sqlDate . "'  AND location = 1";
				$result = mysqli_query($con, $queryString);//returns FALSE if query fails
				$db_array = mysqli_fetch_array($result);
				//store the info from the SQL query in variables. Note that we don't need the info indexes 1 and 2 (date and                       //location)
				$eventName = $db_array[0];
				$eventStaff = $db_array[3];
				$eventDescription = $db_array[4];
                $button = $actday2 . $monthName . $year . "aubrey"; //make unique button 
               //display AUBREY cell
	           echo "<td class = title $class BGCOLOR =E0E0FF><b>$actday2</b>
               <i><b><div align = 'center'>Aubrey</div></b></i>";
			   //if there is a result from the DB for this day and location then print the info
               if (isset($db_array))
			    {
                    echo "<table border = '1' align = 'center' bgcolor = #ADD6FF>";
                    $eventArray = explode(",", $eventStaff);
                 
                    for($k=0; $k < sizeof($eventArray); $k++)
                    {
                        if($eventArray[$k] == $_SESSION['username'])
                        {
                            echo "<tr width = '100%'><td style=\"width: 10%\" bgcolor = #5CADFF><b><u><div align =                                             'center'>$eventName</div></u></b></td></tr>";
				            echo "<tr width = '100%'><td style=\"width: 10%\" bgcolor = #ADD6FF><div align =                                                   'center'>$eventDescription</div></td></tr>";
                        }
                    }
                    echo "</table>";
                }
              
                echo "</td>";
                if(isset($_POST[$button]))
                {
                    $_SESSION['location'] = "AUBREY"; //Pass event details to add_event.php
                    $_SESSION['day'] = $actday2;
                    $_SESSION['month'] = $month;
                    $_SESSION['year'] = $year;
                    $_SESSION['test'] = "AUBREY" . ": " . $month . "-" . $actday2 . "-" . $year;
                    ?>
                    <script>location.href='add_event.php'</script>
                    <?php
                    exit;
                }
	       }
           echo '</tr>';    
	   } 
	   $tempday = $actday; //temp value to hold starting $actday val
	   //Now display the rest of the month
	   if ($actday < $lastDay['mday'])
       {
	       echo '<tr>';  //for new row
	       for ($i=0; $i<7;$i++)
           {
	           $actday++;
			   //create SQL formatted string for looking up date in database
               if ($actday < 10)
			   {//if the day is a single digit we will need a '0' in front of the day
					$sqlDate = $year . "-" . $month . "-" . "0" . $actday;
               }
				else 
				{
					$sqlDate = $year . "-" . $month . "-" . $actday;
			    }
				$queryString = "SELECT * FROM events WHERE date = '" . $sqlDate . "'  AND location = 0";
				$result = mysqli_query($con, $queryString);//returns FALSE if query fails
				$db_array = mysqli_fetch_array($result);
				//print_r($db_array);
			    $eventName = $db_array[0];
				//$eventDate = $db_array[1];//not needed
				//$eventLocation = $db_array[2];//not needed
				$eventStaff = $db_array[3];
				$eventDescription = $db_array[4];
	            if ($actday == $day) 
                {
	               $class = ' class="actday"';
	            }
                else 
                {
	               $class = '';
	            }
	            //DISPLAY DENTON
	            if ($actday <= $lastDay['mday'])
                {
	                echo "<td class = title $class BGCOLOR = FFE0C2><b>$actday</b>
                    <i><b><div align = 'center'>Denton</div></b></i>"; 
					//if there is a result from the DB for this day and location then print the info
					 if (isset($db_array))
			         {
                         echo "<table border = '1' align = 'center' bgcolor = #FFD119>"; //if event exists then make a cell
                         $eventArray = explode(",", $eventStaff);
               
                         for($k=0; $k < sizeof($eventArray); $k++)
                         {
                            if($eventArray[$k] == $_SESSION['username'])
                            {
                       
				                echo "<tr width = '100%'><td style=\"width: 10%\" bgcolor = #E6B800><b><u><div align =                                             'center'>$eventName</div></u></b></td></tr>";
                                echo "<tr width = '100%'><td style=\"width: 10%\" bgcolor = #FFD119><div align =                                                   'center'>$eventDescription</div></td></tr>";
                            }
                         }
                         echo "</table>";
                     }
                     echo "</td>";
                     
	            }
	            else 
                {
                   echo '<td BGCOLOR = F5F5DC  >&nbsp</td>'; 
	            }
	       } 
	       echo '</tr>';
           echo '<tr>'; 
           $actday = $tempday; //set $actday to original value
	       for ($i=0; $i<7;$i++)
           {
	           $actday2++; //aubrey count increment
			   //create SQL formatted string for looking up date in database
				if ($actday2 < 10)
			    {//if the day is a single digit we will need a '0' in front of the day
					$sqlDate = $year . "-" . $month . "-" . "0" . $actday2;
				}
				else 
				{
					$sqlDate = $year . "-" . $month . "-" . $actday2;
			    }
				$queryString = "SELECT * FROM events WHERE date = '" . $sqlDate . "'  AND location = 1";
				$result = mysqli_query($con, $queryString);//returns FALSE if query fails
				$db_array = mysqli_fetch_array($result);
				//store the info from the SQL query in variables. Note that we don't need the info indexes 1 and 2 (date and                       //location)
				$eventName = $db_array[0];
				$eventStaff = $db_array[3];
				$eventDescription = $db_array[4];
                $button = $actday2 . $monthName . $year . "aubrey";
	            if ($actday2 == $day) 
                {
	               $class = ' class="actday"';
	            }
                else 
                {
	               $class = '';
	            }
	            if ($actday2 <= $lastDay['mday'])
                {
                     //DISPLAY AUBREY
	                 echo "<td class = title $class BGCOLOR =E0E0FF><b>$actday2</b>
                     <i><b><div align = center>Aubrey</div></b></i>"; 
					 //if there is a result from the DB for this day and location then print the info
				   	 if (isset($db_array))
			         {
                        echo "<table border = '1' align = 'center' bgcolor = #ADD6FF>";
                        $eventArray = explode(",", $eventStaff);
                 
                        for($k=0; $k < sizeof($eventArray); $k++)
                        {
                            if($eventArray[$k] == $_SESSION['username'])
                            {
                                echo "<tr width = '100%'><td style=\"width: 10%\" bgcolor = #5CADFF><b><u><div align =                                             'center'>$eventName</div></u></b></td></tr>";
				                echo "<tr width = '100%'><td style=\"width: 10%\" bgcolor = #ADD6FF><div align =                                                   'center'>$eventDescription</div></td></tr>";
                            }
                        }
                            echo "</table>";
                     }
                     echo "</td>"; //end row
                     if(isset($_POST[$button]))
                     { 
                        $_SESSION['location'] = "AUBREY"; //Pass event details to add_event.php
                        $_SESSION['day'] = $actday;
                        $_SESSION['month'] = $month;
                        $_SESSION['year'] = $year;
                        $_SESSION['test'] = "AUBREY" . ": " . $month . "-" . $actday2 . "-" . $year;
                        ?>
                        <script>location.href='add_event.php'</script>
                        <?php
                        exit;
                     }
	               
                }
                else 
                {
                    echo '<td BGCOLOR = F5F5DC  >&nbsp</td>'; 
	            }
           }  
	   } 
       echo '</tr>';
	   echo '</table>';
    }
        //DISPLAY "NEXT MONTH" and "LAST MONTH" buttons
    ?>
        <h1 align = 'center' ><font face = 'Edwardian Script ITC'>Your Monthly Schedule</font></h1>
        <form method = "POST" action = "admin_main_menu.php">
            <input value = "Home" type = "submit" style = "float: left;">
        </form>
        <form method="POST" action="">
            <input value ="Last Month" name = "lastMonth" type="submit" style = "float: left;" >
            <!-- pass 0,0,0,1, 0, currentMonth-1, year, day, month, 0 today into                   showCalendar() function-->
        </form>
        
        <form method="POST" action="">
            <input value ="Next Month" name = "nextMonth" type="submit" style = "float: right;">
        </form>
    <?php

    //if last month of next month buttons are pushed then decrement of increment month value. This also takes into account the year
    //place values into showCalendar function
    session_start();
    if(isset($_POST["lastMonth"]))
    {
        if($_SESSION['formMonth'] > 1)
        {
            $_SESSION['formMonth'] = $_SESSION['formMonth']-1;
            showCalendar($_SESSION['formYear'], $_SESSION['formMonth'], $_SESSION['formDay']);
        }
        else if($_SESSION['formMonth'] == 1)
        {
            $_SESSION['formMonth'] = 12;
            $_SESSION['formYear'] = $_SESSION['formYear']-1;
            showCalendar($_SESSION['formYear'], $_SESSION['formMonth'], $_SESSION['formDay']);
            
        }
    }
    else if(isset($_POST["nextMonth"]))
    {
        if($_SESSION['formMonth'] <12)
        {
            $_SESSION['formMonth'] = $_SESSION['formMonth']+1;
            showCalendar($_SESSION['formYear'], $_SESSION['formMonth'], $_SESSION['formDay']);
        }
        else if($_SESSION['formMonth'] >= 12)
        { 
            $_SESSION['formMonth'] = 1;
            $_SESSION['formYear'] = $_SESSION['formYear']+1;
            showCalendar($_SESSION['formYear'], $_SESSION['formMonth'], $_SESSION['formDay']);
           
        }
    }
    else if($_SESSION['formMonth'] != "" && $_SESSION['formYear'] != "") //set so that when buttons clicked page does not reload 
                                                                        //current month
    {
        showCalendar($_SESSION['formYear'], $_SESSION['formMonth'], $_SESSION['formDay']);
    }
  
    //loop through 31 locations of buttonArray to check if button pushed. If pushed then send month, day, year, location to             add_event page  

	?>
	</body>
	</html>